#include <stdio.h>
#include "list.h"

//==================================================================================

#define NAMESIZE 32

//用户自定义的数据结构
typedef struct score_st
{
	int id;
	char name[NAMESIZE];
	int math;
	int chinese;
}score;

//由用户书写的回调函数
static void print_s(const void *record)
{
	//void *不能进行取值操作，转换成用户的数据类型
	const score *r = record;
	printf("学号：%d 姓名：%s 数学：%d 语文：%d\n", r->id, r->name, r->math, r->chinese);
}
static int id_cmp(const void *key, const void *record)
{
	const int *k = key;
	const score *r = record;
	return (*k - r->id);
}
static int name_cmp(const void *key, const void *record)
{
	const char *k = key;
	const score *r = record;
	return strcmp(k, r->name);
}
//==================================================================================

int main(void)
{
	LIST *handler = list_create(sizeof(score));
	if (handler == NULL)
		exit(1);
	score tmp; //用户自定义的数据类型
	for (int i = 0; i < 7; i++)
	{
		tmp.id = i;
		snprintf(tmp.name, NAMESIZE, "stu%d", i);
		tmp.math = rand() % 100;
		tmp.chinese = rand() % 100;
		int ret = list_insert(handler, &tmp, LIST_FORWARD);
		if (ret)
			exit(1);
	}
	list_show(handler, print_s);

	printf("delete id = 3\n");
	int id = 3;
	int ret = list_delete(handler, &id, id_cmp);
	if (ret)
		printf("list_delete id failed\n");
	list_show(handler, print_s);

	printf("delete name = stu6\n");
	const char *name = "stu6";
	ret = list_delete(handler, name, name_cmp);
	if (ret)
		printf("list_delete name failed\n");
	list_show(handler, print_s);

	printf("find id = 30\n");
	int id1 = 30;
	struct score *data;
	data = list_find(handler, &id1, id_cmp);
	if (data == NULL)
		printf("Can not find\n");
	else
		print_s(data);
	
	list_destory(&handler);
	//printf("%p",handler->head.next);  //链表成功销毁则报段错误

	return 0;
}
